package org.NooLab.openNLP.utilities;

import java.util.ArrayList;
import java.util.Vector;

import org.NooLab.structures.nlp.PoStagged;
import org.NooLab.utilities.strings.StringsUtil;

public class PhraseContext {
 
	LanguageProcessorTools lptools = new LanguageProcessorTools();
	
	ArrayList<String> avoidPoSset, deviancePoSset ;
	ArrayList<PoStagged> completePhrase ;

	int action = 0;
	int startofContext = -1;
	int endofContext   = -1;
	int stwLen = 2;
	
	StringsUtil strgutil = new StringsUtil();
	
	
	
	public PhraseContext( ArrayList<PoStagged> completephrase){
		completePhrase = completephrase;
	}
	
	 
	public void clear(){
		completePhrase.clear();
		avoidPoSset.clear();
		deviancePoSset.clear();
	}
	
	public int particleContext( int startindex , int direction){
		int result=-1;
		int p=0, soc;
		String syntag = "";
		
		PoStagged ptag ;
		
		 
		if (direction<-1){
			direction = -1;
		}
		if (direction>1){
			direction = 1;
		}
		
		if (direction==0){
			direction = -1;
			startofContext = particleContext(startindex, direction);
			soc = startofContext ;
			direction = 1;
			endofContext = particleContext(startindex, direction);
			
			if ((endofContext < -1) || (startofContext < -1)){
				result = -3 ; 
			}else{
				result = 0;
				startofContext = soc ;
			}
			return result ;
		}
		
		p = startindex ;
		while ((p>=0) && (p<completePhrase.size())){
			
			if ((p+direction>=0) && (p+direction<completePhrase.size())){
				ptag = completePhrase.get(p+direction) ;
				syntag = ptag.getSyntacticalTag() ;
			}else{
				if (direction<0){
					startofContext = p;
					result = startofContext;
					return result;
				}
				if (direction>0){
					endofContext = completePhrase.size();
					result = endofContext;
					return result;
				}
			}
			if ( strgutil.isMemberofSimpleSet( syntag, deviancePoSset, stwLen) == false){
				if (direction<0){
					startofContext = p + direction+1;
					result = startofContext;
				}
				if (direction>0){
					endofContext = p + direction-1;
					result = endofContext;
				}
				break;
			}
			p = p + direction ;
		} // while
		 
		
		return result;
	}
	
	// ------------------------------------------------------------------------
	
	public void setPoSavoidance( ArrayList<String> avoidsosset ){
		
		avoidPoSset = avoidsosset; 
	}
	
	public void setPoSdeviance( ArrayList<String> avoidsosset ){
		
		deviancePoSset = avoidsosset; 
	}


	public int getStwLen() {
		return stwLen;
	}


	public void setStwLen(int stwLen) {
		this.stwLen = stwLen;
	}


	public int getAction() {
		return action;
	}


	public void setAction(int action) {
		this.action = action;
	}


	public int getStartofContext() {
		return startofContext;
	}


	public void setStartofContext(int startofContext) {
		this.startofContext = startofContext;
	}


	public int getEndofContext() {
		return endofContext;
	}


	public void setEndofContext(int endofContext) {
		this.endofContext = endofContext;
	}
 
	
}
